﻿Imports System.Xml
Imports System.Data
Imports System.Collections.ObjectModel

Public Class Petrol
    Dim frmIsDirty As Boolean = False
    Dim petrolDB As New IETrackerDataClassesDataContext 'GaragePCDataClassesDataContext ' ' 
    Private _pageIsValid As Boolean = True
    Dim al As New ArrayList
    Dim editFlag As Integer

    Sub pageIsValid(ByVal sender As String, ByVal isValid As Boolean)
        Dim inArray As Boolean = False
        For i = 0 To al.Count - 1
            If al.Item(i) = sender Then
                If isValid Then
                    al.Remove(Trim(sender))
                End If
                inArray = True
                Exit For
            End If
        Next
        If Not inArray Then
            If Not isValid Then
                al.Add(Trim(sender))
            End If
        End If
        If al.Count > 0 Then
            _pageIsValid = False
        Else
            _pageIsValid = True
        End If
    End Sub

    Private Sub Petrol_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Dim reader As XmlTextReader = New XmlTextReader("http://rss.weather.com.au/nsw/rosemeadow")
        'Dim ds As DataSet = New DataSet()
        'ds.ReadXml(reader)
        loadVehicleList()
        LoadPetrolList()
        LoadSubHeader()

        mainOverlay.Visibility = Visibility.Visible
        frmIsDirty = False
        setButtonProperties()
    End Sub

    Private Sub Ribbon_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Ribbon.Loaded
        Ribbon.ApplicationMenu = Nothing
    End Sub

    Sub LoadPetrolList()
        Dim petrolTransactions = From petrol In petrolDB.Petrols.AsEnumerable _
                                 Join vehicle In petrolDB.Vehicles _
                                 On petrol.Vehicle Equals vehicle.MainId _
                                 Select petrol, vehicle _
                                 , regoNumber = FormatDateTime(petrol.Date.ToString, DateFormat.ShortDate) & " - $" & petrol.Amount _
                                 , vehicleDetails = vehicle.RegoNumber & " - " & vehicle.Make & "/" & vehicle.Model _
                                 , image = IIf(IO.File.Exists(vehicle.image), vehicle.image, "../Resources/Classic_66_32.png") _
                                 , transactionID = petrol.MainId

        'Dim petrolSource As ObservableCollection = petrolTransactions.OrderByDescending(Function(x) x.petrol.Date)

        listBoxPetrol.ItemsSource = petrolTransactions.OrderByDescending(Function(x) x.petrol.Date)
        listBoxPetrol.SelectedValuePath = "transactionID"
        listBoxPetrol.SelectedIndex = -1

        If petrolTransactions.Count > 1 Then
            ExpanderText.Text = petrolTransactions.Count & " records found"
        Else
            ExpanderText.Text = petrolTransactions.Count & " record found"
        End If
    End Sub

    Sub loadVehicleList()
        Dim vehicleList = From vehicle In petrolDB.Vehicles.AsEnumerable _
                          Select vehicle, vehicle.RegoNumber, image = IIf(IO.File.Exists(vehicle.image), vehicle.image, "../Resources/Classic_66_32.png"), vehicleDDLItems = vehicle.RegoNumber, MainId = vehicle.MainId

        comboboxVehicle.ItemsSource = vehicleList
        comboboxVehicle.SelectedValuePath = "MainId"
        comboboxVehicle.SelectedIndex = -1
    End Sub

    Sub setButtonProperties()
        If frmIsDirty Then
            RibbonButtonSave.IsEnabled = True
            RibbonButtonCancel.IsEnabled = True
            RibbonButtonDelete.IsEnabled = True
            'RibbonButtonNew.IsEnabled = False
        Else
            RibbonButtonSave.IsEnabled = False
            RibbonButtonCancel.IsEnabled = False
            RibbonButtonDelete.IsEnabled = False
            'RibbonButtonNew.IsEnabled = True
        End If
    End Sub

    Private Sub RibbonButtonNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonNew.Click
        mainOverlay.Visibility = Visibility.Collapsed
        editFlag = HelperClass.EditMode.Add

        listBoxPetrol.SelectedIndex = -1
        HelperClass.ClearTextBox(gridPetrolDetails)
        HelperClass.ClearComboBox(gridPetrolDetails)
        DatePickerPetrol.SelectedDate = Nothing

        frmIsDirty = True
        setButtonProperties()
    End Sub


    Private Sub RibbonButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonCancel.Click

        If editFlag = HelperClass.EditMode.Add Then
            mainOverlay.Visibility = Visibility.Visible
            HelperClass.ClearTextBox(gridPetrolDetails)
            HelperClass.ClearComboBox(gridPetrolDetails)
            frmIsDirty = False
            setButtonProperties()
        ElseIf editFlag = HelperClass.EditMode.Edit Then
            Dim selectedPetrolItem As Integer = listBoxPetrol.SelectedValue '.petrol.mainid
            Dim selectedPetrolTransaction = From selectedPetrol In petrolDB.Petrols _
                                            Where selectedPetrol.MainId = selectedPetrolItem
            With selectedPetrolTransaction.First
                DatePickerPetrol.SelectedDate = .Date
                comboboxVehicle.SelectedValue = .Vehicle
                TextBoxDistance.Text = .Distance
                TextBoxLitres.Text = .Litres
                TextBoxAmount.Text = .Amount
                TextBoxAmountPerLitre.Text = .AmountPerLitre
            End With
            frmIsDirty = True
            setButtonProperties()
        End If
    End Sub

    Private Sub RibbonButtonSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonSave.Click
        pageIsValid(DatePickerPetrol.Name, EntryValidation.ValidateDatePicker(DatePickerPetrol))
        pageIsValid(comboboxVehicle.Name, EntryValidation.ValidateCombo(comboboxVehicle))
        pageIsValid(TextBoxAmount.Name, EntryValidation.ValidateText(TextBoxAmount))

        If _pageIsValid Then
            Try
                If editFlag = HelperClass.EditMode.Add Then
                    Dim saveNewPetrolTransaction As New Petrol
                    Dim saveNewTransaction As New Transaction

                    With saveNewTransaction
                        .TransactionAmount = TextBoxAmount.Text
                        .TransactionDate = DatePickerPetrol.SelectedDate
                        .TransactionTypeID = 1
                    End With
                    petrolDB.Transactions.InsertOnSubmit(saveNewTransaction)
                    petrolDB.SubmitChanges()

                    With saveNewPetrolTransaction
                        .Date = DatePickerPetrol.SelectedDate
                        .Distance = TextBoxDistance.Text
                        .Vehicle = comboboxVehicle.SelectedValue
                        .Litres = TextBoxLitres.Text
                        .Amount = TextBoxAmount.Text
                        .AmountPerLitre = TextBoxAmountPerLitre.Text
                        .TransactionID = saveNewTransaction.TransactionID
                    End With

                    petrolDB.Petrols.InsertOnSubmit(saveNewPetrolTransaction)
                    petrolDB.SubmitChanges()
                ElseIf editFlag = HelperClass.EditMode.Edit Then
                    Dim selectedPetrolItem As Integer = listBoxPetrol.SelectedValue '.petrol.mainid
                    Dim editTransaction = From petrol In petrolDB.Petrols _
                                 Join trans In petrolDB.Transactions _
                                 On petrol.MainId Equals trans.TransactionID _
                                 Where petrol.MainId = selectedPetrolItem

                    With editTransaction.First
                        .petrol.Amount = TextBoxAmount.Text
                        .petrol.Date = DatePickerPetrol.SelectedDate
                        .petrol.Distance = TextBoxDistance.Text
                        .petrol.Vehicle = comboboxVehicle.SelectedValue
                        .petrol.Litres = TextBoxLitres.Text
                        .petrol.AmountPerLitre = TextBoxAmountPerLitre.Text

                        .trans.TransactionDate = DatePickerPetrol.SelectedDate
                        .trans.TransactionAmount = TextBoxAmount.Text
                    End With
                    petrolDB.SubmitChanges()

                    Dim petrolTransactions = From petrol In petrolDB.Petrols.AsEnumerable _
                                Join vehicle In petrolDB.Vehicles _
                                On petrol.Vehicle Equals vehicle.MainId _
                                Select petrol, vehicle _
                                , regoNumber = FormatDateTime(petrol.Date.ToString, DateFormat.ShortDate) & " - $" & petrol.Amount _
                                , vehicleDetails = vehicle.RegoNumber & " - " & vehicle.Make & "/" & vehicle.Model _
                                , image = IIf(IO.File.Exists(vehicle.image), vehicle.image, "../Resources/Classic_66_32.png") _
                                , transactionID = petrol.MainId


                    listBoxPetrol.ItemsSource = petrolTransactions.OrderByDescending(Function(x) x.petrol.Date)
                    'listBoxPetrol.SelectedValuePath = "transactionID"
                    listBoxPetrol.SelectedValue = selectedPetrolItem
                End If
               
                LoadSubHeader()
                frmIsDirty = False
                setButtonProperties()

            Catch ex As Exception
                MessageBox.Show("Error encountered while saving the record." & ControlChars.Lf & ControlChars.Lf & ex.message.tostring)
            End Try
        End If
    End Sub


    Private Sub listBoxPetrol_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles listBoxPetrol.SelectionChanged
        Dim lbox As ListBox = sender

        If lbox.IsLoaded Then
            mainOverlay.Visibility = Visibility.Collapsed
            Dim selectedPetrolItem As Integer
            selectedPetrolItem = listBoxPetrol.SelectedValue '.petrol.mainid



            Dim selectedPetrolTransaction = From selectedPetrol In petrolDB.Petrols _
                                            Where selectedPetrol.MainId = selectedPetrolItem
            With selectedPetrolTransaction.First
                DatePickerPetrol.SelectedDate = .Date
                comboboxVehicle.SelectedValue = .Vehicle
                TextBoxDistance.Text = .Distance
                TextBoxLitres.Text = .Litres
                TextBoxAmount.Text = .Amount
                TextBoxAmountPerLitre.Text = .AmountPerLitre
            End With
            editFlag = HelperClass.EditMode.Edit
            frmIsDirty = True
            setButtonProperties()
        Else
            HelperClass.ClearTextBox(gridPetrolDetails)
            HelperClass.ClearComboBox(gridPetrolDetails)
        End If
        'If listBoxPetrol.SelectedIndex > -1 Then

        '    mainOverlay.Visibility = Visibility.Collapsed
        '    Dim selectedPetrolItem As Integer
        '    selectedPetrolItem = listBoxPetrol.SelectedValue.petrol.mainid



        '    Dim selectedPetrolTransaction = From selectedPetrol In petrolDB.Petrols _
        '                                    Where selectedPetrol.MainId = selectedPetrolItem
        '    With selectedPetrolTransaction.First
        '        DatePickerPetrol.SelectedDate = .Date
        '        comboboxVehicle.SelectedValue = .Vehicle
        '        TextBoxDistance.Text = .Distance
        '        TextBoxLitres.Text = .Litres
        '        TextBoxAmount.Text = .Amount
        '        TextBoxAmountPerLitre.Text = .AmountPerLitre
        '    End With
        '    editFlag = HelperClass.EditMode.Edit
        '    frmIsDirty = True
        '    setButtonProperties()
        'Else
        '    HelperClass.ClearTextBox(gridPetrolDetails)
        '    HelperClass.ClearComboBox(gridPetrolDetails)
        '    'mainOverlay.Visibility = Visibility.Visible
        '    'DatePickerPetrol.SelectedDate = Nothing
        'End If
    End Sub


    Private Sub comboboxVehicle_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles comboboxVehicle.SelectionChanged
        pageIsValid(comboboxVehicle.Name, EntryValidation.ValidateCombo(comboboxVehicle))
    End Sub

    Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles searchBox.TextChanged
        Dim petrolTransactions = From petrol In petrolDB.Petrols.AsEnumerable _
                                 Join vehicle In petrolDB.Vehicles _
                                 On petrol.Vehicle Equals vehicle.MainId _
                                 Where (vehicle.RegoNumber & vehicle.Make & vehicle.Model).Contains(searchBox.Text) _
                                 Select petrol, vehicle _
                                 , regoNumber = FormatDateTime(petrol.Date.ToString, DateFormat.ShortDate) & " - $" & petrol.Amount _
                                 , vehicleDetails = vehicle.RegoNumber & " - " & vehicle.Make & "/" & vehicle.Model _
                                 , image = IIf(IO.File.Exists(vehicle.image), vehicle.image, "../Resources/Classic_66_32.png")



        listBoxPetrol.ItemsSource = petrolTransactions.OrderByDescending(Function(x) x.petrol.Date)
        If petrolTransactions.Count > 1 Then
            ExpanderText.Text = petrolTransactions.Count & " records found"
        Else
            ExpanderText.Text = petrolTransactions.Count & " record found"
        End If
        listBoxPetrol.SelectedIndex = 0
    End Sub

    Private Sub DatePickerPetrol_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles DatePickerPetrol.SelectedDateChanged
        pageIsValid(DatePickerPetrol.Name, EntryValidation.ValidateDatePicker(DatePickerPetrol))
        frmIsDirty = True
        setButtonProperties()
    End Sub

    Private Sub TextBoxAmount_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxAmount.TextChanged
        pageIsValid(TextBoxAmount.Name, EntryValidation.ValidateIsNumeric(TextBoxAmount, True))
        frmIsDirty = True
        setButtonProperties()
    End Sub

    Private Sub TextBoxDistance_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxDistance.TextChanged
        pageIsValid(TextBoxDistance.Name, EntryValidation.ValidateIsNumeric(TextBoxDistance, False))
        frmIsDirty = True
        setButtonProperties()
    End Sub

    Private Sub TextBoxLitres_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxLitres.TextChanged
        pageIsValid(TextBoxLitres.Name, EntryValidation.ValidateIsNumeric(TextBoxLitres, False))
        frmIsDirty = True
        setButtonProperties()
    End Sub

    Private Sub TextBoxAmountPerLitre_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxAmountPerLitre.TextChanged
        pageIsValid(TextBoxAmountPerLitre.Name, EntryValidation.ValidateIsNumeric(TextBoxAmountPerLitre, False))
        frmIsDirty = True
        setButtonProperties()
    End Sub

    Sub LoadSubHeader()
        Dim petrolSummary = From pSum In petrolDB.Petrols.AsEnumerable Order By pSum.Date Descending

        LabelFirstTransaction.Content = petrolSummary.Last.Date
        LabelLatestTransaction.Content = petrolSummary.First.Date
        LabelTotalTransaction.Content = "Total : " & FormatCurrency(petrolSummary.Sum(Function(x) x.Amount))
    End Sub

    Private Sub RibbonButtonDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonDelete.Click

    End Sub

    
End Class

